import plotly.io as pio
pio.renderers.default = "notebook"
from dwave.system import DWaveSampler, EmbeddingComposite
from schedule import *
import neal
import plotly.express as px
from tabu import TabuSampler
from dwave_qbsolv import QBSolv
from anneal_solver import solvelog_dwave
#Flag damit nicht unbeabsichtigt der quantumcomputer verwendet wird
use_quantum=True
#maximale Zeit
s = Schedule(time_max=10)
# maximal 3 maschinen
# bitte nur benötigte anzahl der maschinen angeben, wir haben keinen preprocessing step um unnötige maschinen heraus zufiltern
s.build_machines(3)
#Jobs
# 1:(2,3) bedeuted auf der maschine 1 wird die arbeit 2 zeiteinheiten am stück durchgeführt und 3 mal wiederholt
# es sollte wenn möglich nur die dauer auf 1 begrenzt sein, da wir keine zeit hatten die anderen optionen gut zu testen
# frühst möglicher anfangszeitpunkte können zwar gesetzt werden, dies bricht zurzeit noch einige feature und sollte deswegen nicht gemacht werden
s.create_job({1:(1,3),0:(2,3)},parallel_operations=1)
s.create_job({1:(1,3),0:(1,1)})
# s.create_job({1:(1,3),2:(1,1)})
s.create_job({1:(1,1)}, deadline=2)
print(s)
---------------------------- Arbeitsplan Job 0 Operationen: Machine(nr=1, name='m1', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 3 Machine(nr=0, name='m0', parallel_operations=1, start_time=0) Dauer: 2 Anzahl: 3 Job 1 Operationen: Machine(nr=1, name='m1', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 3 Machine(nr=0, name='m0', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Job 2 Operationen: Machine(nr=1, name='m1', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 deadline: 2 ----------------------------
qubo=Qubo(s)
#start term
qubo.penalty_terms[1]=1
# nur eine maschine/job pro job/maschine gleichzeiting
qubo.penalty_terms[2]=1
qubo.penalty_terms[3]=1
# 4 hat keine bedeutung, war ursprünglich für die deadlines vorgesehen
# da wir diese aber ohne strafterme lösen, ist dieses feld leer
# Just in time
qubo.penalty_terms[5]=0
qubo.calculate_qubo()
print(f"qbits: {len(qubo.h)}")
# qubo berechnung falls mehrere Maschinen oder Jobs gleichzeitig verwendet werden sollten - (dies muss im schedule angegeben werden)
# qubo.calculate_qubo_virtuell()
qbits: 69
qubo.plot_qubo()
qubo.plot_connections_qubo_states()
qubo.plot_qubo_terms()
response = QBSolv().sample_qubo(qubo.J, num_repeats=100)
sol3 = response.samples()[0]
*temp, _ = qubo.interpret_solution_dict(sol3)
print(temp)
qubo.plot_solution()
[[0, 2, 4, 15, 16, 17, 36, 40, 42, 44, 65], [state(j:0,m:0,t:0), state(j:0,m:0,t:2), state(j:0,m:0,t:4), state(j:0,m:1,t:6), state(j:0,m:1,t:7), state(j:0,m:1,t:8), state(j:1,m:0,t:6), state(j:1,m:1,t:0), state(j:1,m:1,t:2), state(j:1,m:1,t:4), state(j:2,m:1,t:1)], -29.0, {1: -29, 2: 0, 3: 0, 4: 0}, 29]
samples = TabuSampler().sample_qubo(qubo.J, num_reads=100)
sol2 = samples.samples()[0]
*temp, _ = qubo.interpret_solution_dict(sol2)
print(temp)
qubo.plot_solution()
[[3, 5, 8, 9, 11, 16, 37, 45, 48, 49, 65], [state(j:0,m:0,t:3), state(j:0,m:0,t:5), state(j:0,m:0,t:8), state(j:0,m:1,t:0), state(j:0,m:1,t:2), state(j:0,m:1,t:7), state(j:1,m:0,t:7), state(j:1,m:1,t:5), state(j:1,m:1,t:8), state(j:1,m:1,t:9), state(j:2,m:1,t:1)], -29.0, {1: -29, 2: 0, 3: 0, 4: 0}, 29]
sampler = neal.SimulatedAnnealingSampler()
sampleset = sampler.sample_qubo(qubo.J, num_reads=100)
sol1=sampleset.samples()[0]
*temp,_=qubo.interpret_solution_dict(sol1)
print(temp)
qubo.plot_solution()
[[0, 6, 8, 12, 13, 14, 33, 42, 46, 47, 64], [state(j:0,m:0,t:0), state(j:0,m:0,t:6), state(j:0,m:0,t:8), state(j:0,m:1,t:3), state(j:0,m:1,t:4), state(j:0,m:1,t:5), state(j:1,m:0,t:3), state(j:1,m:1,t:2), state(j:1,m:1,t:6), state(j:1,m:1,t:7), state(j:2,m:1,t:0)], -29.0, {1: -29, 2: 0, 3: 0, 4: 0}, 29]
if use_quantum:
answer = solvelog_dwave(qubo)
*temp2, _ = qubo.interpret_solution_dict(
{x: y for x, y in answer.samples()[0].items()})
print(temp2)
qubo.plot_solution()
[[2, 5, 7, 9, 13, 18, 39, 65, 42, 46, 48], [state(j:0,m:0,t:2), state(j:0,m:0,t:5), state(j:0,m:0,t:7), state(j:0,m:1,t:0), state(j:0,m:1,t:4), state(j:0,m:1,t:9), state(j:1,m:0,t:9), state(j:2,m:1,t:1), state(j:1,m:1,t:2), state(j:1,m:1,t:6), state(j:1,m:1,t:8)], -29.0, {1: -29, 2: 0, 3: 0, 4: 0}, 29]
if use_quantum:
samplerq = DWaveSampler(solver=dict(qpu=True))
samplerq = EmbeddingComposite(samplerq)
init_qbits=answer.samples()[0]
reverse_schedule = [[0.0, 1.0], [10.0, 0.0], [20, 1.0]]
reverse_anneal_params = dict(anneal_schedule=reverse_schedule,
initial_state=init_qbits,
reinitialize_state=True)
reverse_response=samplerq.sample_qubo(
{x: y for x, y in qubo.J.items() if y != 0}, num_reads=1000, **reverse_anneal_params)
*temp3, _ = qubo.interpret_solution_dict(
{x: y for x, y in answer.samples()[0].items()})
print(temp3)
qubo.plot_solution()
[[2, 5, 7, 9, 13, 18, 39, 65, 42, 46, 48], [state(j:0,m:0,t:2), state(j:0,m:0,t:5), state(j:0,m:0,t:7), state(j:0,m:1,t:0), state(j:0,m:1,t:4), state(j:0,m:1,t:9), state(j:1,m:0,t:9), state(j:2,m:1,t:1), state(j:1,m:1,t:2), state(j:1,m:1,t:6), state(j:1,m:1,t:8)], -29.0, {1: -29, 2: 0, 3: 0, 4: 0}, 29]